﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;

using NUnit.Framework;

using ElfDoc;
using ElfDocHelpers;
using DocumentModelFake;

namespace ElfDoc
{

    [TestFixture]
    class DocumentConvolutionIntegrationTest
    {

        #region TestHelpers
        public string GetElfDocTestPath()
        {

            //-Code

            string appBase = AppDomain.CurrentDomain.BaseDirectory;
            string appBaseRoot = appBase.Replace("\\bin", "");
            appBaseRoot = appBaseRoot.Replace("\\Debug", "");

            string testDocPath = Path.Combine(appBaseRoot , @"DocumentTemplate\ElfDocTest\DocumentConvolution");

            //-Return
            return testDocPath; 
        }

        public string GetTargetOUtFileName(string templateSourceFilePathName )
        {
            //-Code
            string fileName = "Out_" + templateSourceFilePathName + "_" + String.Format("{0:yyyyMMdd-HHmmss}", DateTime.Now) ;
            //-Return
            return fileName  ;
        }

        public DocumentModel GetBasicStandardElfDocTestDocumentModel()
        {
            //-Code
            var model = new DocumentModel();
            //-Return
            return model; 
        }
        #endregion 

        [Test]
        [ExpectedException(typeof(System.ArgumentException))]
        public void method_convolve_should_except_on_blank_filenames()
        {

            //-Arrange

            //-Act

            //-Setup a hcml document model; case data and other variable data that is transformed by the template ; 
            var model = new DocumentModel();
            var documentConvolution = new DocumentConvolution { Source = "", Target = "" };

            //-Assert
            documentConvolution.Convolve(model);

        }

        [Test]
        public void method_convolve_of_dynamic_when_value_of_model_CaseID_is_known_guid_then_should_poke_that_known_guid_to_output_target_file()
        {

            // source document template
            var templateSourceFilePathName = @"ConvolutionOfDynamic_ContentControl";

            // source document path 
            string testDocPath = testDocPath = this.GetElfDocTestPath();

            // source and target path names
            var sourceFilePathName = Path.Combine(testDocPath, templateSourceFilePathName);
            var targetFilePathName = Path.Combine(testDocPath, GetTargetOUtFileName(templateSourceFilePathName));

            // source and target path names
            string sourceFilePathnameExt = Path.Combine(testDocPath, templateSourceFilePathName + ".docx");
            string targetFilePathnameExt = Path.Combine(testDocPath, targetFilePathName + ".docx"); ;

            // Setup a document model. The document model is the variable data used to poke particular information to a target document using a source document template.
            dynamic model = this.GetBasicStandardElfDocTestDocumentModel();

            // set CaseID model property 
            model.CaseID = GuidExt.ZeroGuid();

            // set CaseConductor model property 
            model.CaseConductor = new CaseConductor();
            model.CaseConductor.Load(model.CaseID);

            // the values we are looking to be poked into the document
            var n0 = model.CaseConductor.CaseNoteList[0].NoteText;
            var n1 = model.CaseConductor.CaseNoteList[1].NoteText;
            var n2 = model.CaseConductor.CaseNoteList[2].NoteText; 

            // dotted context ...
            Dictionary<string, int> dottedContext = new Dictionary<string, int>();
            dottedContext.Add("CaseConductor.CaseNoteList", 1 ) ;
            model.DottedContext = dottedContext;

            var noteDottedContext = model.ValueByKeyTuple("CaseConductor.CaseNoteList[.].NoteText");

            //-Act

            // exec a document convolution 
            var documentConvolution = new DocumentConvolution { Source = sourceFilePathnameExt, Target = targetFilePathnameExt };
            documentConvolution.Convolve(model);

            //-Assert 

            // look for the caseID on the document, it should be in the output file the convolution kicked out
            // ...

        }

        [Test]
        public void method_convolve_when_value_of_model_CaseID_is_known_guid_then_should_poke_that_known_guid_to_output_target_file()
        {

            // source document template
            var templateSourceFilePathName = @"TestDocTemplate_1_ContentControl";

            // source document path 
            string testDocPath = testDocPath = this.GetElfDocTestPath(); 

            // source and target path names
            var sourceFilePathName = Path.Combine(testDocPath, templateSourceFilePathName);
            var targetFilePathName = Path.Combine(testDocPath, GetTargetOUtFileName(templateSourceFilePathName));

            // source and target path names
            string sourceFilePathnameExt = Path.Combine(testDocPath, templateSourceFilePathName + ".docx");
            string targetFilePathnameExt = Path.Combine(testDocPath, targetFilePathName + ".docx"); ;

            // Setup a document model. The document model is the variable data used to poke particular information to a target document using a source document template.
            var model = this.GetBasicStandardElfDocTestDocumentModel();

            // set CaseID model property 
            model.CaseID = GuidExt.ZeroGuid() ;

            // set CaseConductor model property 
            model.CaseConductor = new CaseConductor();
            model.CaseConductor.Load(model.CaseID);

            //-Act

            // exec a document convolution 
            var documentConvolution = new DocumentConvolution{ Source = sourceFilePathnameExt , Target = targetFilePathnameExt } ;
            documentConvolution.Convolve(model);

            //-Assert 

            // look for the caseID on the document, it should be in the output file the convolution kicked out
            // ...

        }

        [Test]
        public void method_contentControl_GetControlID_should_return_unique_int_per_content_control()
        {

            // source document template
            var templateSourceFilePathName = @"TestDocTemplate_ContentControl_TitleAndTagAndControlID";

            // source document path 
            string testDocPath = testDocPath = this.GetElfDocTestPath();

            // source and target path names
            var sourceFilePathName = Path.Combine(testDocPath, templateSourceFilePathName);
            var targetFilePathName = Path.Combine(testDocPath, GetTargetOUtFileName(templateSourceFilePathName));

            // source and target path names
            string sourceFilePathnameExt = Path.Combine(testDocPath, templateSourceFilePathName + ".docx");
            string targetFilePathnameExt = Path.Combine(testDocPath, targetFilePathName + ".docx"); ;

            // Setup a document model. The document model is the variable data used to poke particular information to a target document using a source document template.
            var model = this.GetBasicStandardElfDocTestDocumentModel();

            //-Act

            // exec a document convolution 
            var documentConvolution = new DocumentConvolution { Source = sourceFilePathnameExt, Target = targetFilePathnameExt };
            var contentControlsList = documentConvolution.GetListOfAllContentControls();

            //-Assert 

            // REFACTOR : unfinished test here no asserts ...
            foreach (var contentControl in contentControlsList)
            {
                // should be unique number ...
                var contentControlcontrolID = contentControl.GetControlID();
            }

        }

        [Test]
        public void method_contentControl_GetAliasGet_should_return_CaseID_AliasTitle()
        {

            // source document template
            var templateSourceFilePathName = @"TestDocTemplate_ContentControl_TitleAndTagAndControlID";

            // source document path 
            string testDocPath = testDocPath = this.GetElfDocTestPath();

            // source and target path names
            var sourceFilePathName = Path.Combine(testDocPath, templateSourceFilePathName);
            var targetFilePathName = Path.Combine(testDocPath, GetTargetOUtFileName(templateSourceFilePathName));

            // source and target path names
            string sourceFilePathnameExt = Path.Combine(testDocPath, templateSourceFilePathName + ".docx");
            string targetFilePathnameExt = Path.Combine(testDocPath, targetFilePathName + ".docx"); ;

            // Setup a document model. The document model is the variable data used to poke particular information to a target document using a source document template.
            var model = this.GetBasicStandardElfDocTestDocumentModel();

            //-Act

            // exec a document convolution 
            var documentConvolution = new DocumentConvolution { Source = sourceFilePathnameExt, Target = targetFilePathnameExt };
            var contentControlsList = documentConvolution.GetListOfAllContentControls();

            //-Assert 

            // REFACTOR : unfinished test here and no asserts ...
            foreach (var contentControl in contentControlsList)
            {
                // should be CaseID_AliasTitle
                var contentControlAliasTitle = contentControl.GetAlias();
            }

        }

        [Test]
        public void method_contentControl_GetTag_should_return_CaseID_Tag()
        {

            // source document template
            var templateSourceFilePathName = @"TestDocTemplate_ContentControl_TitleAndTagAndControlID";

            // source document path 
            string testDocPath = testDocPath = this.GetElfDocTestPath();

            // source and target path names
            var sourceFilePathName = Path.Combine(testDocPath, templateSourceFilePathName);
            var targetFilePathName = Path.Combine(testDocPath, GetTargetOUtFileName(templateSourceFilePathName));

            // source and target path names
            string sourceFilePathnameExt = Path.Combine(testDocPath, templateSourceFilePathName + ".docx");
            string targetFilePathnameExt = Path.Combine(testDocPath, targetFilePathName + ".docx"); ;

            // Setup a document model. The document model is the variable data used to poke particular information to a target document using a source document template.
            var model = this.GetBasicStandardElfDocTestDocumentModel();

            //-Act

            // exec a document convolution 
            var documentConvolution = new DocumentConvolution { Source = sourceFilePathnameExt, Target = targetFilePathnameExt };
            var contentControlsList = documentConvolution.GetListOfAllContentControls();

            //-Assert 

            // REFACTOR : unfinished test here no asserts ...
            foreach (var contentControl in contentControlsList)
            {
                var contentControlTag = contentControl.GetTag();
            }

        }

    }

}
